﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mindfor.Web.Modules.Yandex.Controllers
{
	/// <summary>
	/// Yandex Map widget controller.
	/// 
	/// Provides small static map with link on bigger dynamic JS-map
	/// </summary>
	public class YandexController : CmsController
	{
		/// <summary>
		/// Loads static image from Yandex maps.
		/// </summary>
		/// <param name="param">Parametres to create map (Latitude,Longitude,Title)</param>
		/// <returns>Partial view with size from param</returns>
		[Widget("Map")]
		public ActionResult MapWidget(string param)
		{
			if ((String.IsNullOrEmpty(param)) || (String.IsNullOrEmpty(Module.Options[YandexModule.ApiKeyOption].GetValue<string>())))
				throw new ArgumentNullException("param");

			string[] widgetParams = param.Split(',');

			string coordinates = widgetParams[0] + "," + widgetParams[1];

			//default values
			//size - size in pixels: width*height
			//z - zoom (1 - min (world), 17 - max (house))
			//l - type of map ("map" - default scheme, "pmap" - narodnaya karta, "sat" - satelite, "sat,skl" - hybryd sat+map)
			//pt - identifies options for pointer (coordinates -> view)
			string defaults = "&size=200,200&z=16&l=map&pt=";

			ViewData["apiKey"] = Module.Options[YandexModule.ApiKeyOption].GetValue<string>();
			ViewData["userMap"] = Module.Options[YandexModule.UserMapParametrOption].GetValue<string>();
			ViewData["linkTarget"] = Module.Options[YandexModule.WidgetLinkTargetOption].GetValue<string>();
			ViewData["coordinates"] = coordinates;
			ViewData["title"] = widgetParams[2];
			ViewData.Model = "ll=" + coordinates + defaults + coordinates + ",pm2rdm&key=" + Module.Options[YandexModule.ApiKeyOption].GetValue<string>(); ;
			return PartialView();
		}
	}
}